package com.dongan.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongan.pojo.Codes;
import com.dongan.pojo.File;
import com.dongan.pojo.Users;
import com.dongan.service.CodesService;
import com.dongan.service.FileService;
import com.dongan.service.impl.FileServiceImpl;
import com.dongan.utils.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * @author longTao
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {
    @Resource
    private FileService service;
    @Autowired
    private CodesService codesService;

    /**
     * 查询产品
        @param designProcedureTag 生产工序标签
        @param designModuleTag 物料设计标签
      * @param type 商品类型
      * @param checkTag 审核编号
     * @param firstId 一级分类查询
     * @param secondId 二级分类查询
     * @param threeId 三级分类查询
     * @param name 根据名字模糊查询
     * @param pageNum  页码
     * @param pageSize 页码大小
     * @param deleted 是否删除

     * @return
     */
   @PreAuthorize("hasAuthority('/productFileQuery')")
    @GetMapping(value = "/queryProduct")
    public ResultResponseUtil queryProduct(String designProcedureTag,String designModuleTag,String type,String checkTag,String firstId,String secondId,String threeId,String name,String pageNum,String pageSize,String deleted){
        QueryWrapper<File> queryWrapper=new QueryWrapper<>();
        Page<File> filePage=new Page<>();
        if (!StringUtils.isEmpty(firstId)){
            queryWrapper.eq("FIRST_KIND_ID",Long.valueOf(firstId));
        }
        if (!StringUtils.isEmpty(checkTag)){
            queryWrapper.eq("CHECK_TAG",checkTag);
        }
        if (!StringUtils.isEmpty(secondId)){
            queryWrapper.eq("SECOND_KIND_ID",Long.valueOf(secondId));
        }
        if (!StringUtils.isEmpty(threeId)){
            queryWrapper.eq("THIRD_KIND_ID",Long.valueOf(threeId));
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("PRODUCT_NAME",name);
        }
        if (!StringUtils.isEmpty(designModuleTag)){
            queryWrapper.like("DESIGN_MODULE_TAG",designModuleTag);
        }
        if (!StringUtils.isEmpty(designProcedureTag)){
            queryWrapper.like("DESIGN_PROCEDURE_TAG",designProcedureTag);
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum="1";
        }
        if (StringUtils.isEmpty(pageSize)){
            pageSize="2";
        }
        if (!StringUtils.isEmpty(type)){
            if(type.equals("1")){
                type="Y001-2";
                queryWrapper.eq("TYPE",type);
            }else{
                queryWrapper.eq("TYPE",type);
            }
        }
        if (!StringUtils.isEmpty(checkTag)){
            queryWrapper.eq("CHECK_TAG",checkTag);
        }
        if (!StringUtils.isEmpty(deleted)){
            if (deleted.equals("1")){
                queryWrapper.eq("deleted",deleted);
            }
        }
        filePage.setSize(Long.valueOf(pageSize));
        filePage.setCurrent(Long.valueOf(pageNum));
        Page<File> page = service.page(filePage, queryWrapper);
        return new ResultResponseUtil(true,page);
    }

    /**
     * 添加产品
     * @param file
     * @param request
     * @return
     */
    @PreAuthorize("hasAuthority('/productRegistration')")
    @PostMapping("/addFile")
    public Object addFile(@RequestBody File file, HttpServletRequest request){
        String pID=100+file.getFirstKindId()+file.getSecondKindId()+file.getThirdKindId()
                + NumberFormatUtil.numberFormat(6, SerialNumber.fileSN);
        file.setProductId(pID);
        file.setCheckTag("S001-0");file.setChangeTag("D002-0");
        file.setPriceChangeTag("J001-0");file.setFileChangeAmount(0);
        file.setDesignModuleTag("W001-0");file.setDesignProcedureTag("G001-0");
        file.setDesignCellTag("K001-0");
        Users users =(Users) CommonProp.user;
        System.out.println("users = " + users);
        file.setRegister(users.getLoginId());
        file.setRegisterTime(GetSysDateTime.getDatetime());
        boolean flag=service.saveOrUpdate(file);
        String msg="登记失败!";
        if(flag){
            SerialNumber.fileSN=SerialNumber.fileSN+1;
            Codes codes=new Codes();
            codes.setId(1L);
            codes.setDefValue(Integer.valueOf(SerialNumber.fileSN).toString());
            codesService.updateById(codes);
            msg="登记成功!";
        }
        return new ResultResponseUtil(flag,msg);
    }

    /**
     * 审核
     * @param file
     * @return
     */
    @PreAuthorize("hasAuthority('/productFileReview')")
    //@PreAuthorize("hasPermission(#file,'/productFileReview')")
    @PostMapping("/checkFile")
    public Object checkFile(@RequestBody File file){
        file.setCheckTime(GetSysDateTime.getDatetime());
        file.setCheckTag("S001-1");
        Users users =(Users) CommonProp.user;
        file.setChecker(users.getLoginId());
        boolean flag=service.updateById(file);
        String msg="审核失败!";
        if(flag){
            msg="审核成功!";
        }
        return new ResultResponseUtil(flag,msg);
    }

    /**
     * 修改文件
     * @param file
     * @return
     */
    @PreAuthorize("hasAuthority('/productFileChanges')")
    @PostMapping("/updateFile")
    public Object updateFile(@RequestBody File file){
        file.setCheckTag("S001-0");file.setFileChangeAmount(file.getFileChangeAmount()+1);
        Users users =(Users) CommonProp.user;
        file.setChanger(users.getLoginId());
        file.setChangeTime(new Date());
        file.setUpdateTime(null);
        boolean flag=service.updateById(file);
        String msg="修改失败!";
        if(flag){
            msg="修改成功!";
        }
        return new ResultResponseUtil(flag,msg);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @GetMapping("/deleteFile")
    @PreAuthorize("hasAuthority('/productFileDeletion')")
    public Object deleteFile(Long id){
        boolean flag=service.removeById(id);
        String msg="删除失败!";
        if(flag){
            msg="删除成功!";
        }
        return new ResultResponseUtil(flag,msg);
    }

    /**
     * 恢复所有文件
     * @param id
     * @return
     */
    @GetMapping("/restoreFile")
    @PreAuthorize("hasAuthority('/productFileRecovery')")
    public Object restoreFileFile(Long id){
        boolean flag=service.restore(id);
        String msg="恢复失败!";
        if(flag){
            msg="恢复成功!";
        }
        return new ResultResponseUtil(flag,msg);
    }

    /**
     * 物理删除
     * @param id
     * @return
     */
    @GetMapping("/recoveryFile")
    @PreAuthorize("hasAuthority('/permanentlyDeleteProductFiles')")
    public Object recoveryCFile(Long id){
        boolean flag=service.recoveryCFile(id);
        String msg="删除失败!";
        if(flag){
            msg="删除成功!";
        }
        return new ResultResponseUtil(flag,msg);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/queryByID")
    @PreAuthorize("hasAuthority('/productFileQuery')")
    public Object queryByPRODUCT_ID(Long id){
        File file=service.getById(id);
        return new ResultResponseUtil(true,"查询成功!",file);
    }

    /**
     * 删除时查询
     * @param designModuleTag
     * @param type
     * @param checkTag
     * @param firstId
     * @param secondId
     * @param threeId
     * @param name
     * @param pageNum
     * @param pageSize
     * @param deleted
     * @return
     */
    @GetMapping(value = "/queryAll")
    @PreAuthorize("hasAuthority('/productFileQuery')")
    public ResultResponseUtil queryAll(String designModuleTag,String type,String checkTag,String firstId,String secondId,String threeId,String name,String pageNum,String pageSize,String deleted){
       Integer a=null;
       Integer b=null;
       Integer c=null;
        if (StringUtils.isEmpty(designModuleTag)){
            designModuleTag="";
        }
        if (StringUtils.isEmpty(type)){
            type="";
        }
        if (StringUtils.isEmpty(checkTag)){
            checkTag="";
        }
        if (!StringUtils.isEmpty(firstId)){
           a=Integer.valueOf(firstId);
        }
        if (!StringUtils.isEmpty(secondId)){
            b=Integer.valueOf(secondId);
        }
        if (!StringUtils.isEmpty(threeId)) {
            c =Integer.valueOf(threeId);
        }
        if (StringUtils.isEmpty(name)){
            name="";
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum="1";
        }
        if (StringUtils.isEmpty(pageSize)){
            pageSize=String.valueOf(CommonProp.pageSiez);
        }
        if (!StringUtils.isEmpty(type)){
            if(type.equals("1")){
                type="Y001-2";
            }else{
                type="Y001-1";
            }
        }
        if (!StringUtils.isEmpty(deleted)){
            if (!deleted.equals("1")){
                deleted="0";
            }
        }
        List<File> list = service.queryTheDataThatHasBeenDeleted(designModuleTag, type, checkTag,a, b, c, name, Integer.valueOf(deleted), Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        PageInfo<File> pageInfo=new PageInfo<>(list);
        System.out.println("pageInfo = " + pageInfo);
        return new ResultResponseUtil(true,pageInfo);
    }
}

